package com.manor.main.login;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.manor.common.activity.BaseLocationActivity;
import com.manor.common.utils.RouteUtil;
import com.manor.common.utils.ScreenDimenUtil;
import com.manor.common.utils.StaIntentUtil;
import com.manor.main.R;

import org.jetbrains.annotations.NotNull;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

@Route(path = RouteUtil.PATH_LOGIN)
public class LoginActivity extends BaseLocationActivity {

    public static final String REGEX_PSWD = "^(\\S){8,12}$";
    public static final String REGEX_PHONE = "^[0-9]{11}$";
    @Autowired(name = VCodeVerifyFragment.FLAG_FROM_TYPE)
    int mFromType;
    @Autowired(name = VCodeVerifyFragment.FLAG_TITLE_TEXT)
    String mTitleText;
    @Autowired(name = VCodeVerifyFragment.FLAG_BTN_TEXT)
    String mButtonText;
    @Autowired(name = VCodeVerifyFragment.FLAG_PHONE)
    String mPhoneText;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void main(Bundle savedInstanceState) {
        super.main(savedInstanceState);
        handleIntent(getIntent());
        int statusBarHeight = ScreenDimenUtil.getInstance().getStatusBarHeight();
        FrameLayout flContainer = findViewById(R.id.fl_container);
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) flContainer.getLayoutParams();
        layoutParams.topMargin = statusBarHeight;
        flContainer.setLayoutParams(layoutParams);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handleIntent(intent);
    }

    public void handleIntent(Intent intent) {
        if (intent != null) {
            if (intent.hasExtra(VCodeVerifyFragment.FLAG_FROM_TYPE)) {
                mFromType = intent.getIntExtra(VCodeVerifyFragment.FLAG_FROM_TYPE, 1);
            }
            if (intent.hasExtra(VCodeVerifyFragment.FLAG_TITLE_TEXT)) {
                mTitleText = intent.getStringExtra(VCodeVerifyFragment.FLAG_TITLE_TEXT);
            }
            if (intent.hasExtra(VCodeVerifyFragment.FLAG_BTN_TEXT)) {
                mButtonText = intent.getStringExtra(VCodeVerifyFragment.FLAG_BTN_TEXT);
            }
            if (mFromType == 3) {
                for (Fragment fragment : getSupportFragmentManager().getFragments()) {
                    getSupportFragmentManager().beginTransaction().remove(fragment).commit();
                }
                if (intent.hasExtra(VCodeVerifyFragment.FLAG_PHONE)) {
                    mPhoneText = intent.getStringExtra(VCodeVerifyFragment.FLAG_PHONE);
                }
                loadForgetPassword();
            } else if (mFromType == 4) {
                loadCodeFragment();
            } else {
                addFragment();
            }
        }
    }

    public void loadCodeFragment() {
        SmsLoginFragment vCodeLoginFragment = (SmsLoginFragment) getSupportFragmentManager().findFragmentByTag(SmsLoginFragment.class.getSimpleName());
        if (vCodeLoginFragment == null) {
            vCodeLoginFragment = SmsLoginFragment.newInstance();
        }
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (!vCodeLoginFragment.isAdded()) {
            ft.add(R.id.fl_container, vCodeLoginFragment, SmsLoginFragment.class.getSimpleName());
//            ft.addToBackStack(SmsLoginFragment.class.getSimpleName());
        } else {
            ft.show(vCodeLoginFragment);
        }
//        ft.commit();
        ft.commitAllowingStateLoss();
    }

    /**
     * 加载注冊登录fragment
     */
    private void addFragment() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        LauncherLoginFragment launcherLoginFragment = (LauncherLoginFragment) getSupportFragmentManager()
                .findFragmentByTag(LauncherLoginFragment.class.getSimpleName());
        if (launcherLoginFragment == null) {
            launcherLoginFragment = LauncherLoginFragment.newInstance();
        }
        if (!launcherLoginFragment.isAdded()) {
            ft.add(R.id.fl_container, launcherLoginFragment, LauncherLoginFragment.class.getSimpleName());
        } else {
            ft.show(launcherLoginFragment);
        }
        ft.commitAllowingStateLoss();
    }

    protected void loadForgetPassword() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        VCodeVerifyFragment vCodeVerifyFragment = (VCodeVerifyFragment) getSupportFragmentManager()
                .findFragmentByTag(VCodeVerifyFragment.class.getSimpleName());
        if (vCodeVerifyFragment == null) {
            vCodeVerifyFragment = VCodeVerifyFragment.newInstance();
        }
        Bundle args = new Bundle();
        args.putInt(VCodeVerifyFragment.FLAG_FROM_TYPE, mFromType);
        args.putString(VCodeVerifyFragment.FLAG_TITLE_TEXT, mTitleText);
        args.putString(VCodeVerifyFragment.FLAG_BTN_TEXT, mButtonText);
        args.putString(VCodeVerifyFragment.FLAG_PHONE, mPhoneText);
        vCodeVerifyFragment.setArguments(args);
        if (vCodeVerifyFragment.isAdded()) {
            ft.show(vCodeVerifyFragment);
        } else {
            ft.add(R.id.fl_container, vCodeVerifyFragment, VCodeVerifyFragment.class.getSimpleName());
        }

        ft.commitAllowingStateLoss();
    }

    public void dealAgreement(TextView textView) {
        String content = textView.getText().toString();
        if (TextUtils.isEmpty(content)) {
            return;
        }

        SpannableStringBuilder ssb = new SpannableStringBuilder();
        ssb.append(textView.getText());
        final int start = content.indexOf("《");//第一个出现的位置
        ssb.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NotNull View widget) {
                RouteUtil.forwardPath(RouteUtil.PC_WEB, StaIntentUtil.REGISTRATION_AGREEMENT);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.parseColor("#F3941C"));       //设置文件颜色
                ds.setUnderlineText(false);
            }
        }, start, start + 10, 0);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setText(ssb, TextView.BufferType.SPANNABLE);
    }

    public void goBackLogin() {
        for (Fragment fragment : getSupportFragmentManager().getFragments()) {
            getSupportFragmentManager().popBackStack();// beginTransaction().remove(fragment).commit();
        }
//        replaceFragment();

        SmsLoginFragment vCodeLoginFragment = (SmsLoginFragment) getSupportFragmentManager().findFragmentByTag(SmsLoginFragment.class.getSimpleName());
        if (vCodeLoginFragment == null) {
            vCodeLoginFragment = SmsLoginFragment.newInstance();
        }
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.fl_container, vCodeLoginFragment, SmsLoginFragment.class.getSimpleName());
        ft.commitAllowingStateLoss();
    }

    /**
     * 加载注冊登录fragment
     */
    private void replaceFragment() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        LauncherLoginFragment launcherLoginFragment = (LauncherLoginFragment) getSupportFragmentManager()
                .findFragmentByTag(LauncherLoginFragment.class.getSimpleName());
        if (launcherLoginFragment == null) {
            launcherLoginFragment = LauncherLoginFragment.newInstance();
        }
        //        if (launcherLoginFragment.isAdded()) {
        //            ft.show(launcherLoginFragment);
        //        } else {
        //            ft.add(R.id.fl_container, launcherLoginFragment, LauncherLoginFragment.class.getSimpleName());
        //        }
        ft.replace(R.id.fl_container, launcherLoginFragment, LauncherLoginFragment.class.getSimpleName());
        ft.commitAllowingStateLoss();
    }

}